//
//  XMJigsawController.m
//  XiaoMiao
//
//  Created by 23 on 2018/2/5.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMJigsawController.h"
#import "XMImageContainerView.h"
#import "XMImageToolView.h"
#import "XMJigsawShareController.h"

@interface XMJigsawController () <XMImageToolViewDelegate>

/**containerView*/
@property(nonatomic,weak) XMImageContainerView *containerView;
/**toolView*/
@property(nonatomic,weak) XMImageToolView *toolView;

@end

@implementation XMJigsawController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupUI];
    
    
    //设置假数据
//    UIImage *image1 = [UIImage imageNamed:@"ic_camera_back"];
//    UIImage *image2 = [UIImage imageNamed:@"ic_camera_blue"];
//    UIImage *image3 = [UIImage imageNamed:@"ic_camera_confirm"];
//    UIImage *image4 = [UIImage imageNamed:@"ic_camera_huazhuang"];
//    UIImage *image5 = [UIImage imageNamed:@"ic_camera_kouqiang"];
//    UIImage *image6 = [UIImage imageNamed:@"ic_camera_meirong"];
//    self.containerView.imagesArray = @[image1, image2, image3, image4, image5, image6];
    self.containerView.imagesArray = self.imagesArray;
    
}

#pragma mark - UI
- (void)setupUI
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.title = @"照片对比";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(actionSave)];
    
    
    XMImageContainerView *containerView = [[XMImageContainerView alloc] init];
    [self.view addSubview:containerView];
    self.containerView = containerView;
    
    XMImageToolView *toolView = [XMImageToolView imageToolView];
    toolView.delegate = self;
    [self.view addSubview:toolView];
    self.toolView = toolView;
    
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(40);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.toolView.mas_top);
    }];
    
    [toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(94);
    }];
}


#pragma mark - action
- (void)actionSave
{
    UIImage *image = [self.containerView snapImage];
    
    
    XMJigsawShareController *vc = [[XMJigsawShareController alloc] init];
    vc.snapImage = image;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - XMImageToolViewDelegate
/**样式状态改变的回调*/
- (void)imageToolView:(XMImageToolView *)toolView didStyleStatusChanged:(XMImageToolStatus)styleStatus
{
    switch (styleStatus) {
        case XMImageToolStatusInitial:
        {
            //初始化
            NSLog(@"------>初始化状态");
            [self.containerView enterNormal];
        }
            break;
        case XMImageToolStatusMask:
        {
            //马赛克
            NSLog(@"------>马赛克状态");
            [self.containerView enterMaskStatus];
        }
            break;
        case XMImageToolStatusAttach:
        {
            //文字水印
            NSLog(@"------>文字水印状态");
        }
            break;
        case XMImageToolStatusEraser:
        {
            //橡皮擦
            NSLog(@"------>橡皮擦状态");
            [self.containerView enterEraser];
        }
            break;
        case XMImageToolStatusClearAttach:
        {
            //清除文字水印
            NSLog(@"------>清除文字水印状态");
            [self.containerView clearAttach];
        }
            break;
        default:
            break;
    }
}

//马赛克/橡皮擦状态下  前进
- (void)imageToolView:(XMImageToolView *)toolView styleStatusDidForward:(XMImageToolStatus)styleStatus
{
    if (styleStatus == XMImageToolStatusMask) {
        //添加马赛克
        [self.containerView forwardMask];
    }
}

//马赛克/橡皮擦状态下  后退
- (void)imageToolView:(XMImageToolView *)toolView styleStatusDidBackward:(XMImageToolStatus)styleStatus
{
    if (styleStatus == XMImageToolStatusMask) {
        //撤销马赛克
        [self.containerView backMask];
    }
}

//确认添加的文字水印
- (void)imageToolView:(XMImageToolView *)toolView didConfirmText:(NSString *)text
{
    if (text.length == 0) {
        return;
    }
    
    [self.containerView addText:text];
    toolView.attachTextField.text = @"";
}


@end
